package cn.xiuxius.askbox.config;

import cn.xiuxius.askbox.AskBoxApplication;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Configuration;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

@Configuration
@Slf4j
public class ApplicationConfiguration implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        initConfigFile();
    }

    private static void initConfigFile() {
        File dir = new File("config");

        if (!dir.exists()) {
            dir.mkdir();
        }

        File appConfig = new File("config/application.yml");
        File settingConfig = new File("config/application-dev.yml");

        try {
            if (!appConfig.exists()) {
                InputStream in = AskBoxApplication.class.getClassLoader().getResourceAsStream("application.yml");
                if (in != null) {
                    Files.copy(in, appConfig.toPath());
                }
            }
            if (!settingConfig.exists()) {
                InputStream in = AskBoxApplication.class.getClassLoader().getResourceAsStream("application-dev.yml");
                if (in != null) {
                    Files.copy(in, settingConfig.toPath());
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

}
